package kau.dao;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Course;

public class CourseDAOImpl implements CourseDAOIF {

	DBUtil db = null;
	private static CourseDAOImpl instance;
	
	private CourseDAOImpl() {
		db = DBUtil.getInstance();
	}
	public static CourseDAOImpl getInstance(){
		if( instance == null) instance = new CourseDAOImpl();
		return instance;
	}
	
	private boolean isExist(int courseId) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try{
			result = session.selectOne("dbConfig.CourseMapper.isExist",courseId);
			System.out.println("result ::"+result);
		}finally{
			session.close();
		}
		if(result!= null) flag= true;
		
		
		
		return flag;
	}
	
	@Override
	public void insert(Course course) throws DuplicationException {
		if(isExist(course.getCourseId())) throw new DuplicationException(course.getCourseName());
		SqlSession session = db.getSession();
		try{
			session.insert("dbConfig.CourseMapper.insert", course);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public void update(Course course) throws RecordNotFoundException {
		if(!isExist(course.getCourseId())) throw new RecordNotFoundException(course.getCourseName());
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.CourseMapper.update", course);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public void delete(int courseId) throws RecordNotFoundException {
		if(!isExist(courseId)) throw new RecordNotFoundException(courseId);
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.CourseMapper.delete", courseId);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public Course getCourse(int courseId) throws RecordNotFoundException {
		if(!isExist(courseId)) throw new RecordNotFoundException(courseId);
		SqlSession session = db.getSession();
		Course course=null;
		try{
			course = session.selectOne("dbConfig.CourseMapper.getCourse", courseId);
		}finally{
			session.close();
		}
		return course;
	}

	@Override
	public ArrayList<Course> getAllCourses() {
		ArrayList<Course> courseList = new ArrayList<Course>();
		SqlSession session = db.getSession();
		try{
			courseList = (ArrayList) session.selectList("dbConfig.CourseMapper.getAllCourses");
		} finally{
			session.close();
		}
		return courseList;
	}
	
	public boolean courseIdCheck(int courseId) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try{
			result = session.selectOne("dbConfig.CourseMapper.isExist",courseId);
		}finally{
			session.close();
		}
		if(result!= null) flag= true;
		
		return flag;
	}
	
	@Override
	public int getMaxCourseId() {
		// TODO Auto-generated method stub
		int listCount=0;
		
		SqlSession session = db.getSession();
		
		try {
			Integer temp=null;
			temp=session.selectOne("dbConfig.CourseMapper.getMaxCourseId");
			if(temp!=null)listCount=temp;
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		return listCount;
	}
	@Override
	public String getCourseInstructor(int courseId)
			throws RecordNotFoundException {
		if(!isExist(courseId)) throw new RecordNotFoundException(courseId);
		SqlSession session = db.getSession();
		String custId = null;
		try{
			custId = session.selectOne("dbConfig.CourseMapper.getCourseInstructor", courseId);
		}finally{
			session.close();
		}
		return custId;
	}


}
